package com.ruoyi.common.core.web.page;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.web.domain.AjaxResult;

import java.util.List;

/**
 * 表格分页数据对象
 *
 * @author scihi
 */
public class AjaxPageResult extends AjaxResult {
    private static final long serialVersionUID = 1L;

    private final String totlTag = "total";

    private final String pageNumTag = "pageNum";

    private final String pageSizeTag = "pageSize";

    private final String rowsTag = "rows";

    private AjaxPageResult() {
        super();
        this.setRows(null);//rows等价于父类的data
        this.setTotal(0L);
        this.setPageNum(0);
        this.setPageSize(0);
    }

    public static AjaxPageResult success(Page page){
        AjaxPageResult p = new AjaxPageResult();
        p.setRows(page.getResult());
        p.setTotal(page.getTotal());
        p.setPageNum(page.getPageNum());
        p.setPageSize(page.getPageSize());
        return p;
    }

    public static AjaxPageResult success(String msg, Page page){
        AjaxPageResult p = new AjaxPageResult();
        p.setMsg(msg);
        p.setRows(page.getResult());
        p.setTotal(page.getTotal());
        p.setPageNum(page.getPageNum());
        p.setPageSize(page.getPageSize());
        return p;
    }

    public static AjaxPageResult success(List list){
        AjaxPageResult p = new AjaxPageResult();
        PageInfo page = new PageInfo(list);
        p.setRows(list);
        p.setTotal(page.getTotal());
        p.setPageNum(page.getPageNum());
        p.setPageSize(page.getPageSize());
        return p;
    }

    public static AjaxPageResult success(String msg, List list){
        AjaxPageResult p = new AjaxPageResult();
        p.setMsg(msg);
        PageInfo page = new PageInfo(list);
        p.setRows(list);
        p.setTotal(page.getTotal());
        p.setPageNum(page.getPageNum());
        p.setPageSize(page.getPageSize());
        return p;
    }

    public AjaxPageResult setTotal (long total){
        super.put(totlTag, total);
        return this;
    }

    public AjaxPageResult setPageNum (long pageNum){
        super.put(pageNumTag, pageNum);
        return this;
    }

    public AjaxPageResult setPageSize (long pageSize){
        super.put(pageSizeTag, pageSize);
        return this;
    }

    public AjaxPageResult setRows (List rows){
        super.put(rowsTag, rows);
        return this;
    }

    @Override
    public AjaxPageResult put(String key, Object val){
        super.put(key, val);
        return this;
    }
}
